<?php

/*
	系统URL自定义
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'manage';
$return=array();
$tabs=array(
	array('op'=>'manage','title'=>'管理端URL'),
	array('op'=>'custom','title'=>'用户端URL'),
);
if($_RQ['op']=='manage'){
	$params=array('this'=>1);
	if(!empty($_RQ['diy'])){
		$params['diy like']='%'.trim($_RQ['diy']).'%';
		$return['params']['diy']=$_RQ['diy'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$fields=array('id','display','diy','common','status');
	$list=pdo_getslice('core_url_config',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit_op'=>'manage_edit','list'=>array(
			array('name'=>'diy','title'=>'自定义URL','type'=>'string'),
			array('name'=>'common','title'=>'系统URL','type'=>'string'),
			array('name'=>'display','title'=>'优先级','type'=>'string'),
			array('name'=>'status','title'=>'启用/关闭','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'diy','add_op'=>'manage_edit'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='custom'){
	$params=array('this'=>2);
	if(!empty($_RQ['diy'])){
		$params['diy like']='%'.trim($_RQ['diy']).'%';
		$return['params']['diy']=$_RQ['diy'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$fields=array('id','display','diy','common','status');
	$list=pdo_getslice('core_url_config',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit_op'=>'custom_edit','list'=>array(
			array('name'=>'diy','title'=>'自定义URL','type'=>'string'),
			array('name'=>'common','title'=>'系统URL','type'=>'string'),
			array('name'=>'display','title'=>'优先级','type'=>'string'),
			array('name'=>'status','title'=>'启用/关闭','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'diy','add_op'=>'custom_edit'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='custom_edit'){
	//编辑用户端规则
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'diy'=>trim($post['diy']),
			'common'=>trim($post['common']),
			'display'=>intval($post['display']),
			'status'=>intval($post['status']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('core_url_config',$data,array('id'=>$id));
		}else{
			$data['this']=2;
			pdo_insert('core_url_config',$data);
		}
		exi('操作成功！','','custom');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_url_config',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置URL规则',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'string','name'=>'diy','title'=>'自定义URL'),
			array('type'=>'link','name'=>'common','title'=>'指向系统页面'),
			array('type'=>'string','name'=>'display','title'=>'优先级','help'=>'填写数字，数字越大，优先级越高'),
			array('type'=>'switch','name'=>'status','title'=>'是否启用'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='manage_edit'){
	//编辑用户端规则
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'diy'=>trim($post['diy']),
			'common'=>trim($post['common']),
			'display'=>intval($post['display']),
			'status'=>intval($post['status']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('core_url_config',$data,array('id'=>$id));
		}else{
			$data['this']=1;
			pdo_insert('core_url_config',$data);
		}
		exi('操作成功！','','manage');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_url_config',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置URL规则',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'string','name'=>'diy','title'=>'自定义URL'),
			array('type'=>'choose','name'=>'common','title'=>'指向系统页面','action'=>'system.diy_url','op'=>'choose'),
			array('type'=>'string','name'=>'display','title'=>'优先级','help'=>'填写数字，数字越大，优先级越高'),
			array('type'=>'switch','name'=>'status','title'=>'是否启用'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的规则！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('core_url_config',array('status'=>intval($post['status'])),array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_url_config',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_url_config',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='choose_plugin'){
	if($_RQ['id']){
		$plugin=pdo_get('core_plugins',array('id'=>intval($_RQ['id'])),array('name','menus','covers','title','type'));
			if($plugin['type']!=4){
				$is_new=1;
			}else{
				$is_new=0;
			}
			$list=array();
			$list[]=array('title'=>'插件首页','url'=>$plugin['name'].'/basic.module_home');
			if($plugin['menus']){
				$plugin['menus']=json_decode($plugin['menus'],true);
				foreach($plugin['menus'] as $menu){
					if($plugin['type']!=4){
						$list[]=array('title'=>$menu['title'],'url'=>$plugin['name'].'/'.$menu['name'].'/');
					}else{
						$list[]=array('title'=>$menu['title'],'url'=>'./index.php?action=module&do='.$menu['name'].'&m='.$plugin['name']);
					}
					
				}
			}
			if($plugin['covers']){
				$plugin['covers']=json_decode($plugin['covers'],true);
				foreach($plugin['covers'] as $menu){
					$list[]=array('title'=>$menu['title'].'入口设置','url'=>$plugin['name'].'/basic.module_home/cover/is_new='.$is_new.'&ac='.$menu['name']);
				}
			}
			
			$return['list']=$list?$list:array();
			$return['htmls']=array(
				'type'=>'list',
				'tabs'=>array(
					array('op'=>'choose','title'=>'系统功能'),
					array('op'=>'choose_plugin','title'=>'插件功能'),
				),
				'list'=>array('data'=>'list','delall'=>false,'edit'=>false,'del_one'=>false,'pagelist'=>false,'list'=>array(
					array('name'=>'title','title'=>'页面名称','type'=>'string'),
				),'action'=>array(
					array('type'=>'modal_result','name'=>'url','title'=>'选取'),
				)),
				'search_one'=>array('name'=>'title','add'=>false),
			);
			exi($return);
	}else{
		$pindex=max(1, intval($_RQ['page']));
			$psize=15;
			$params=array('type <'=>4);
			if($_RQ['title']){
				$return['params']['title']=$_RQ['title'];
				$params['title like']='%'.$_RQ['title'].'%';
			}
			$list=pdo_getslice('core_plugins',$params,array($pindex,$psize),$total,array('id','title'));
			$return['list']=$list?$list:array();
			$totalpage=ceil($total/$psize);
			$totalpage=intval($totalpage);
			$return['totalpage'] =$totalpage;
			$return['params']['page'] =$pindex;
			$return['htmls']=array(
				'type'=>'list',
				'tabs'=>array(
					array('op'=>'choose','title'=>'系统功能'),
					array('op'=>'choose_plugin','title'=>'插件功能'),
					array('op'=>'diy_url','title'=>'自定义'),
				),
				'list'=>array('data'=>'list','delall'=>false,'edit'=>false,'del_one'=>false,'list'=>array(
					array('name'=>'title','title'=>'插件名称','type'=>'string'),
				),'action'=>array(
					array('type'=>'modal','op'=>'choose_plugin','title'=>'选择功能'),
				)),
				'search_one'=>array('name'=>'title','add'=>false),
			);
			exi($return);
	}
}
if($_RQ['op']=='choose'){
	$pindex=max(1, intval($_RQ['page']));
			$psize=15;
			$params=array();
			if($_RQ['title']){
				$return['params']['title']=$_RQ['title'];
				$params['title like']='%'.$_RQ['title'].'%';
			}
			$list=pdo_getslice('core_permissions',$params,array($pindex,$psize),$total,array('id','action','op','title'));
			if($list){
				foreach($list as &$row){
					$row=array('title'=>$row['title'],'url'=>'core/'.$row['action'].'/'.$row['op']);
				}
			}
			$return['list']=$list?$list:array();
			$totalpage=ceil($total/$psize);
			$totalpage=intval($totalpage);
			$return['totalpage'] =$totalpage;
			$return['params']['page'] =$pindex;
			$return['htmls']=array(
				'type'=>'list',
				'tabs'=>array(
					array('op'=>'choose','title'=>'系统功能'),
					array('op'=>'choose_plugin','title'=>'插件功能'),
					array('op'=>'diy_url','title'=>'自定义'),
				),
				'list'=>array('data'=>'list','delall'=>false,'edit'=>false,'del_one'=>false,'list'=>array(
					array('name'=>'title','title'=>'页面名称','type'=>'string'),
				),'action'=>array(
					array('type'=>'modal_result','name'=>'url','title'=>'选取'),
				)),
				'search_one'=>array('name'=>'title','add'=>false),
			);
			exi($return);
}
if($_RQ['op']=='diy_url'){
	$return=array();
	
	$return['htmls']=array(
		'type'=>'edit',
		'post'=>'modal_result',
		'result_field'=>'url',
		'desc'=>'自定义链接页面',
		'tabs'=>array(
			array('op'=>'choose','title'=>'系统功能'),
			array('op'=>'choose_plugin','title'=>'插件功能'),
			array('op'=>'diy_url','title'=>'自定义'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'url','title'=>'自定义页面地址','help'=>'填写页面URL，支持系统标准URL书写格式'),
		)
	);
	exi($return);
}
